<template>
  <div :class="['login-wrapper', ['', 'login-form-right', 'login-form-left'][direction]]">
    <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        class="login-form ele-bg-white"
        size="large"
    >
      <h4>用户登录</h4>
      <el-form-item prop="UserName">
        <el-input placeholder="请输入登录账号" v-model="form.UserName" prefix-icon="el-icon-user" clearable/>
      </el-form-item>
      <el-form-item prop="Password">
        <el-input placeholder="请输入登录密码" v-model="form.Password" prefix-icon="el-icon-lock" show-password/>
      </el-form-item>
      <div class="el-form-item" v-if="false">
        <el-checkbox v-model="form.remember">记住密码</el-checkbox>
        <el-link
            @click="$router.push('/forget')"
            type="primary"
            class="ele-pull-right"
            :underline="false">忘记密码
        </el-link>
      </div>
      <div class="el-form-item">
        <el-button @click="doSubmit" :loading="loading" type="primary" class="login-btn" size="large">
          {{ loading ? '登录中' : '登录' }}
        </el-button>
      </div>
    </el-form>
    <div class="login-copyright">copyright © 2021 huijudf.com all rights reserved.</div>
  </div>
</template>

<script>
import setting from '@/config/setting'

export default {
  name: 'login-index',
  data () {
    return {
      // 登录框方向, 0居中, 1居右, 2居左
      direction: 0,
      // 加载状态
      loading: false,
      // 表单数据
      form: {
        UserName: '',
        Password: '',
        remember: true
      },
      // 表单验证规则
      rules: {
        UserName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        Password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
    }
  },
  mounted () {
    if (setting.takeToken()) {
      this.goHome()
    }
  },
  methods: {
    /* 提交 */
    doSubmit () {
      this.$refs['loginForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        let formData = new FormData()
        for (let key in this.form) {
          formData.append(key, this.form[key])
        }
        this.$http.post('/account/password', formData).then((res) => {
          this.loading = false
          if (res.data.code === 200) {
            this.$message.success('登录成功')
            this.$store.dispatch('user/setToken', {
              token: res.data.result.Token,
              remember: this.form.remember
            }).then(() => {
              this.goHome()
            })
          }
        }).catch(() => {
          this.loading = false
        })
      })
    },
    /* 跳转到首页 */
    goHome () {
      const query = this.$route.query
      const path = query && query.from ? query.from : '/'
      this.$router.push(path).catch(() => {
      })
    },
  }
}
</script>

<style scoped>
/* 背景 */
.login-wrapper {
  padding: 50px 20px;
  position: relative;
  box-sizing: border-box;
  background-image: url("~@/assets/img/bg-login.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}

.login-wrapper:before {
  content: "";
  background-color: rgba(0, 0, 0, .2);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 卡片 */
.login-form {
  margin: 0 auto;
  width: 360px;
  max-width: 100%;
  padding: 25px 30px;
  position: relative;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
  border-radius: 4px;
  z-index: 2;
}

.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

.login-form h4 {
  text-align: center;
  margin: 0 0 25px 0;
}

.login-form > .el-form-item {
  margin-bottom: 25px;
}


.login-btn {
  display: block;
  width: 100%;
}

/* 第三方登录图标 */
.login-oauth-icon {
  color: #FFF;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

/* 底部版权 */
.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}

/* 响应式 */
@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -220px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
